namespace QMVnSoft.Controls
{
    using QMVnSoft;
    using QMVnSoft.SystemForms;
    using QMVnSoft.DAL;
    using System;
    using QMVnSoft.Properties;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class MainButtonControl : Button
    {
        private bool ib_UserModulButton;
        private long ii_MenuItemNumber;
        private Image oUserFocusedandMoveImage = Resources.BUTTON;
        private Image oUserFocusedImage = Resources.BUTTON;
        private Image oUserMouseLeaveImage = Resources.BUTTON;
        private Image oUserMouseMoveImage = Resources.BUTTON;

        protected override void OnClick(EventArgs e)
        {
            if ((this.LastButton != null) && !this.LastButton.UserModulButton)
            {
                this.LastButton.BackgroundImage = this.LastButton.UserMouseLeaveImage;
            }
            else if (((this.LastButton != null) && this.LastButton.UserModulButton) && this.UserModulButton)
            {
                this.LastButton.BackgroundImage = this.LastButton.UserMouseLeaveImage;
            }
            if ((this.LastModulButton != null) && this.UserModulButton)
            {
                this.LastModulButton.BackgroundImage = this.LastModulButton.UserMouseLeaveImage;
                this.LastModulButton = this;
            }
            this.BackgroundImage = this.UserFocusedandMoveGroundImage;
            this.UserActiveControl = this;
            if (((AppInstance.UserType == 0) && (this.MenuItemNumber != 0L)) && !AppDataAccess.CheckUserRights(AppInstance.UserName, "M", this.MenuItemNumber, 1))
            {
                MessageBox.Show("Không có quyền truy cập chức năng này!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                base.OnClick(e);
            }
        }

        protected override void OnLeave(EventArgs e)
        {
            this.LastButton = this;
            base.OnLeave(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            if (this.Focused || ((this.UserModulButton && (this.LastModulButton != null)) && (this.LastModulButton == this)))
            {
                this.BackgroundImage = this.UserFocusedImage;
            }
            else
            {
                this.BackgroundImage = this.UserMouseLeaveImage;
            }
            base.OnMouseLeave(e);
        }

        protected override void OnMouseMove(MouseEventArgs mevent)
        {
            if (this.Focused || ((this.UserModulButton && (this.LastModulButton != null)) && (this.LastModulButton == this)))
            {
                this.BackgroundImage = this.UserFocusedandMoveGroundImage;
            }
            else
            {
                this.BackgroundImage = this.UserMouseMoveImage;
            }
            base.OnMouseMove(mevent);
        }

        public void UserClick()
        {
            this.OnClick(EventArgs.Empty);
        }

        public void UserMouseLeave()
        {
            if (this.Focused || ((this.UserModulButton && (this.LastModulButton != null)) && (this.LastModulButton == this)))
            {
                this.BackgroundImage = this.UserFocusedImage;
            }
            else
            {
                this.BackgroundImage = this.UserMouseLeaveImage;
            }
        }

        public void UserMouseMove()
        {
            if (this.Focused || ((this.UserModulButton && (this.LastModulButton != null)) && (this.LastModulButton == this)))
            {
                this.BackgroundImage = this.UserFocusedandMoveGroundImage;
            }
            else
            {
                this.BackgroundImage = this.UserMouseMoveImage;
            }
        }

        public MainButtonControl LastButton
        {
            get
            {
                if (base.FindForm() is FormMain)
                {
                    return (base.FindForm() as FormMain).LastButton;
                }
                return null;
            }
            set
            {
                if ((base.FindForm() is FormMain) && ((base.FindForm() as FormMain).LastButton != value))
                {
                    (base.FindForm() as FormMain).LastButton = value;
                }
            }
        }

        public MainButtonControl LastModulButton
        {
            get
            {
                if (base.FindForm() is FormMain)
                {
                    return (base.FindForm() as FormMain).LastModulButton;
                }
                return null;
            }
            set
            {
                if ((base.FindForm() is FormMain) && ((base.FindForm() as FormMain).LastModulButton != value))
                {
                    (base.FindForm() as FormMain).LastModulButton = value;
                }
            }
        }

        [DefaultValue(0)]
        public long MenuItemNumber
        {
            get
            {
                return this.ii_MenuItemNumber;
            }
            set
            {
                this.ii_MenuItemNumber = value;
            }
        }

        public Control UserActiveControl
        {
            get
            {
                if (base.FindForm() is FormMain)
                {
                    return (base.FindForm() as FormMain).UserActiveControl;
                }
                return null;
            }
            set
            {
                if ((base.FindForm() is FormMain) && ((base.FindForm() as FormMain).UserActiveControl != value))
                {
                    (base.FindForm() as FormMain).UserActiveControl = value;
                }
            }
        }

        public Image UserFocusedandMoveGroundImage
        {
            get
            {
                return this.oUserFocusedandMoveImage;
            }
            set
            {
                this.oUserFocusedandMoveImage = value;
            }
        }

        public Image UserFocusedImage
        {
            get
            {
                return this.oUserFocusedImage;
            }
            set
            {
                this.oUserFocusedImage = value;
            }
        }

        [DefaultValue(false)]
        public bool UserModulButton
        {
            get
            {
                return this.ib_UserModulButton;
            }
            set
            {
                this.ib_UserModulButton = value;
            }
        }

        public Image UserMouseLeaveImage
        {
            get
            {
                return this.oUserMouseLeaveImage;
            }
            set
            {
                this.oUserMouseLeaveImage = value;
            }
        }

        public Image UserMouseMoveImage
        {
            get
            {
                return this.oUserMouseMoveImage;
            }
            set
            {
                this.oUserMouseMoveImage = value;
            }
        }
    }
}

